namespace CIS.Resource
{
    using CISLib;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class ResourceTelephonePageEdit : Page
    {
        protected Image iResource;
        protected Label lbTelephoneCityDdl;
        protected Label lbTelephoneDescrip;
        protected Label lbTelephoneNum;
        protected Label lbTelephoneProvinceDdl;
        protected UserPrincipal Principal = null;
        protected Button SaveBtn;
        protected DropDownList TelephoneCityDdl;
        protected TextBox TelephoneDescripTxb;
        protected TextBox TelephoneNumTxb;
        protected DropDownList TelephoneProvinceDdl;

        private void FillPage()
        {
            if (base.Request.QueryString["IsModify"] == "True")
            {
                Guid id = new Guid(base.Request.QueryString["TelephoneId"]);
                ResourceTelephone resourceTelephone = ManageResource.GetResourceTelephoneInfoViaId(id);
                this.TelephoneNumTxb.Text = resourceTelephone.TelephoneNum;
                this.TelephoneDescripTxb.Text = resourceTelephone.TelephoneDescrip;
                for (int i = 0; i < this.TelephoneProvinceDdl.Items.Count; i++)
                {
                    if (this.TelephoneProvinceDdl.Items[i].Value == resourceTelephone.TelephoneProvince.ToString())
                    {
                        this.TelephoneProvinceDdl.SelectedIndex = i;
                        break;
                    }
                }
                this.TelephoneCityDdl.DataSource = ManageCity.GetCityViaProvinceId(resourceTelephone.TelephoneProvince);
                this.TelephoneCityDdl.DataTextField = "regionName";
                this.TelephoneCityDdl.DataValueField = "regionId";
                this.TelephoneCityDdl.DataBind();
                bool result = false;
                for (int i = 0; i < this.TelephoneCityDdl.Items.Count; i++)
                {
                    if (this.TelephoneCityDdl.Items[i].Value == resourceTelephone.TelephoneCity.ToString())
                    {
                        this.TelephoneCityDdl.SelectedIndex = i;
                        result = true;
                        break;
                    }
                }
                if (!result)
                {
                    this.TelephoneCityDdl.Items.Clear();
                }
            }
        }

        private void InitializeComponent()
        {
            this.TelephoneProvinceDdl.SelectedIndexChanged += new EventHandler(this.TelephoneProvinceDdl_SelectedIndexChanged);
            this.SaveBtn.Click += new EventHandler(this.SaveBtn_Click);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.SetFromImage();
            this.SetFrom();
            this.TelephoneProvinceDdl.DataSource = ManageProvince.GetProvinceNoAll();
            this.TelephoneProvinceDdl.DataTextField = "RegionName";
            this.TelephoneProvinceDdl.DataValueField = "RegionId";
            this.TelephoneProvinceDdl.DataBind();
            this.TelephoneCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.TelephoneProvinceDdl.SelectedItem.Value));
            this.TelephoneCityDdl.DataTextField = "regionName";
            this.TelephoneCityDdl.DataValueField = "regionId";
            this.TelephoneCityDdl.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                this.SelectAuth();
            }
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            string strTelephoneNum = this.TelephoneNumTxb.Text.Trim();
            string strTelephoneDescrip = this.TelephoneDescripTxb.Text.Trim();
            if (base.Request.QueryString["IsModify"] == "True")
            {
                if (strTelephoneNum != "")
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.TelephoneCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.TelephoneCityDdl.SelectedItem.Value);
                    }
                    ManageResource.UpdateResourceTelephoneInfo(new Guid(base.Request.QueryString["TelephoneId"]), strTelephoneNum, strTelephoneDescrip, new Guid(this.TelephoneProvinceDdl.SelectedValue), cityId);
                    base.Response.Redirect("~/Resource/ResourceTelephonePage.aspx", true);
                }
                base.Response.Write("<script> alert('Name is Null !') </script>");
            }
            else if (strTelephoneNum != "")
            {
                if (!ManageResource.IsExistSameResourceTelephoneInfo(strTelephoneNum))
                {
                    Guid cityId = new Guid("00000000-0000-0000-0000-000000000001");
                    if (this.TelephoneCityDdl.Items.Count != 0)
                    {
                        cityId = new Guid(this.TelephoneCityDdl.SelectedItem.Value);
                    }
                    ManageResource.InsertNewResourceTelephoneInfo(strTelephoneNum, strTelephoneDescrip, new Guid(this.TelephoneProvinceDdl.SelectedValue), cityId);
                    base.Response.Redirect("~/Resource/ResourceTelephonePage.aspx", true);
                }
                else
                {
                    base.Response.Write("<script> alert('该信息已存在') </script>");
                }
            }
        }

        private void SelectAuth()
        {
            if (this.Principal.OwnRole.TypeC == 0)
            {
                this.InitPage();
                this.FillPage();
                this.SaveBtn.Visible = false;
            }
            else if (this.Principal.OwnRole.TypeC == 1)
            {
                this.InitPage();
                this.FillPage();
            }
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbTelephoneNum.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTelephonePageEdit", "lbTelephoneNum", language, path);
            this.lbTelephoneDescrip.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTelephonePageEdit", "lbTelephoneDescrip", language, path);
            this.SaveBtn.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTelephonePageEdit", "SaveBtn", language, path);
            if (this.Principal.OwnArea[0].AreaId.ToString() == "a7f26634-10a9-4cb1-aa2c-8c91a8dfdd66")
            {
                this.lbTelephoneProvinceDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTelephonePageEdit", "lbTelephoneProvinceDdl", language, path);
                this.lbTelephoneCityDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTelephonePageEdit", "lbTelephoneCityDdl", language, path);
            }
            else
            {
                this.lbTelephoneProvinceDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTelephonePageEdit", "lbTelephoneProvinceDdl", language, path);
                this.lbTelephoneCityDdl.Text = ManageSystemConfig.GetSystemSetLanguage("ResourceTelephonePageEdit", "lbTelephoneCityDdl", language, path);
            }
        }

        private void SetFromImage()
        {
            switch (this.Session["language"].ToString())
            {
                case "cn":
                    this.iResource.ImageUrl = "../images/title_SharedInfo.jpg";
                    break;

                case "en":
                    this.iResource.ImageUrl = "../images/title_SharedInfoEn.jpg";
                    break;
            }
        }

        private void TelephoneProvinceDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.TelephoneCityDdl.DataSource = ManageCity.GetCityViaProvinceId(new Guid(this.TelephoneProvinceDdl.SelectedItem.Value));
            this.TelephoneCityDdl.DataTextField = "regionName";
            this.TelephoneCityDdl.DataValueField = "regionId";
            this.TelephoneCityDdl.DataBind();
        }
    }
}

